package com.ruoyi.report_tg.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.syrw.annotation.DealHeh;
import com.ruoyi.syrw.annotation.FIllBasicInfo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_tg.domain.bo.TdhslbBsInfoBo;
import com.ruoyi.report_tg.domain.vo.TdhslbBsInfoVo;
import com.ruoyi.report_tg.domain.TdhslbBsInfo;
import com.ruoyi.report_tg.mapper.TdhslbBsInfoMapper;
import com.ruoyi.report_tg.service.ITdhslbBsInfoService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 土的含水率记录Service业务层处理
 *
 * @author zyb
 * @date 2024-05-15
 */
@RequiredArgsConstructor
@Service
@Transactional
public class TdhslbBsInfoServiceImpl implements ITdhslbBsInfoService {

    private final TdhslbBsInfoMapper baseMapper;

    /**
     * 查询土的含水率记录
     */
    @Override
    @FIllBasicInfo
    public TdhslbBsInfoVo queryById(Long excelId){
        return baseMapper.selectVoById(excelId);
    }

    /**
     * 查询土的含水率记录列表
     */
    @Override
    public TableDataInfo<TdhslbBsInfoVo> queryPageList(TdhslbBsInfoBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TdhslbBsInfo> lqw = buildQueryWrapper(bo);
        Page<TdhslbBsInfoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询土的含水率记录列表
     */
    @Override
    public List<TdhslbBsInfoVo> queryList(TdhslbBsInfoBo bo) {
        LambdaQueryWrapper<TdhslbBsInfo> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TdhslbBsInfo> buildQueryWrapper(TdhslbBsInfoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TdhslbBsInfo> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getJilbh()), TdhslbBsInfo::getJilbh, bo.getJilbh());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpmc()), TdhslbBsInfo::getYangpmc, bo.getYangpmc());
        lqw.eq(StringUtils.isNotBlank(bo.getYangpbh()), TdhslbBsInfo::getYangpbh, bo.getYangpbh());
        return lqw;
    }

    /**
     * 新增土的含水率记录
     */
    @Override
    public Boolean insertByBo(TdhslbBsInfoBo bo) {
        TdhslbBsInfo add = BeanUtil.toBean(bo, TdhslbBsInfo.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
        }
        return flag;
    }

    /**
     * 修改土的含水率记录
     */
    @Override
    @DealHeh
    public Boolean updateByBo(TdhslbBsInfoBo bo) {
        TdhslbBsInfo update = BeanUtil.toBean(bo, TdhslbBsInfo.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TdhslbBsInfo entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除土的含水率记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
